<?php
/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/3/16
 * Time: 14:01
 */

namespace Api\Domain;


use Bbs\Api\ForumAttachmentApi;
use Bbs\Api\ForumForumApi;
use Bbs\Api\ForumPostApi;
use Bbs\Api\ForumThreadApi;

class ForumForumDomain extends BaseDomain{

    protected $business_code = 'forumForum';

    protected $allowType = array("json", "rss", "html");

    public function query(){

        $this->checkVersion(100);
        addLog("ForumForum/query", $_GET, $_POST, $this->client_id . "调用论坛版块查询接口!");

        if(IS_POST){

            $page_no = $this -> _post('page_no',1);
            $page_size = $this -> _post('page_size',6);
            $img_num = $this -> _post('img_num',3);

            $page = array('curpage'=>$page_no,'size'=>$page_size);

            $order = "fid asc";

            $result = apiCall(ForumForumApi::QUERY_WITH_COUNT,array(null,$page,$order));

            if($result['status']){
                foreach($result['info']['list'] as $k => $vo){
                    $modnewposts = $vo['modnewposts'];

                    $map = array(
                        'itboye_forum_thread.status' => 1,
                        'itboye_forum_thread.fid' => $vo['fid'],
                    );
                    $map_invisible = "post.invisible='1' OR itboye_forum_forum.modnewposts='0'";
                    $order = "itboye_forum_thread.dateline desc";
                    $page = array('curpage'=>1,'size'=>1);
                    $res = apiCall(ForumThreadApi::QUERY_WITH_COUNT,array($map,$map_invisible,$page,$order));
                    if($res['status']){
                        $result['info']['list'][$k]['dateline'] = $res['info']['list'][0]['dateline'];
                        $result['info']['list'][$k]['author'] = $res['info']['list'][0]['author'];
                        $result['info']['list'][$k]['author_id'] = $res['info']['list'][0]['author_id'];
                        $result['info']['list'][$k]['replies'] = $res['info']['list'][0]['replies'];
                        $result['info']['list'][$k]['likes'] = $res['info']['list'][0]['likes'];
                        $result['info']['list'][$k]['subject'] = $res['info']['list'][0]['subject'];
                        $result['info']['list'][$k]['tid'] = $res['info']['list'][0]['tid'];
                    }else{
                        $this -> apiReturnErr($res['info']);
                    }

                    //获取发表时间展示信息
                    $time = time() - $res['info']['list'][0]['dateline'];
                    $result['info']['list'][$k]['dateline_show'] = getDateInfo($time,$res['info']['list'][0]['dateline']);

                    //取用户信息
                    $result['info']['list'][$k]['user_info'] = $this->getUserInfo($res['info']['list'][0]['author_id']);
                    //获取图片信息
                    $map = array(
                        'tid' => $res['info']['list'][0]['tid'],
                    );
                    $field = "aid,pid";
                    $res = apiCall(ForumAttachmentApi::QUERY_NO_PAGING,array($map,false,$field));
                    if($res['status']){
                        $result['info']['list'][$k]['img'] = array();
                        $i = 0;
                        foreach($res['info'] as $v){
                            //判断附件所属帖子是否能显示（审核通过与否）
                            $post_info = apiCall(ForumPostApi::GET_INFO,array(array('pid'=>$v['pid'])));
                            if($post_info['status']){
                                if(is_null($post_info['info'])){
                                    continue;
                                }elseif($post_info['info']['status']==0 || ($post_info['info']['invisible']==0 && $modnewposts==1)){
                                    //删除的帖子和未通过审核的附件不显示
                                    continue;
                                }
                            }else{
                                continue;
                            }

                            array_push($result['info']['list'][$k]['img'],$v['aid']);
                            $i++;
                            if($i == $img_num){
                                break;
                            }
                        }
                    }else{
                        $this -> apiReturnErr($res['info']);
                    }
                }

                $this -> apiReturnSuc($result['info']);
            }else{
                $this -> apiReturnErr($result['info']);
            }

        }

    }

    /**
     * 查询用户信息
     */
    private function getUserInfo($uid){
        $result = apiCall('Api/BbsUserCenter/userInfo',array($uid),'Domain');
        if($result['status']){
            return $result['info'];
        }else{
            $this->apiReturnErr('查询用户信息失败');
        }
    }

}